<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
	
	var $table = '';
	var $primary_key = 'id';
	
	function create($data)
	{
		$this->db->insert($this->table, $data);
        return $this->db->insert_id();
	}
	
	function update($id, $data)
	{
		if ($id==''|| $id===FALSE)
		{
			return FALSE;
		}
		
		$where = array();
	 	$where[$this->primary_key] = $id;
	 	$this->update_rule($where, $data);
	 	
	 	return TRUE;
	}
	
	function update_rule($where, $data)
	{
	 	$this->db->where($where);
	 	$this->db->update($this->table, $data);
	}

	function del($id)
	{
		if ($id==''|| $id===FALSE)
		{
			return FALSE;
		}
		
		$where = array();
	 	$where[$this->primary_key] = $id;
	 	$this->del_rule($where);
		
		return TRUE;
	}
	
	function del_rule($where)
	{
		if (!$where)
		{
			return FALSE;
		}
		
	 	$this->db->where($where);
		$this->db->delete($this->table);
		
		return TRUE;
	}
	
	function get_id($where = array())
	{
		if (count($where))
		{
	 		$this->db->where($where);
		}
		
	 	$this->db->select($this->primary_key);
		$query = $this->db->get($this->table);
		
		$row = $query->row();
		$id = ($query->num_rows()) ? $row->{$this->primary_key} : FALSE;
		
		return $id;
	}
	
	function get_info($id, $field = '')
	{
		if ($id==''|| $id===FALSE)
		{
			return FALSE;
		}
		
	 	if ($field != '')
	 	{
	 		$this->db->select($field);
	 	}
	 	
	 	$where = array();
	 	$where[$this->primary_key] = $id;
	 	$this->db->where($where);
	 	
		$query = $this->db->get($this->table);
		if ($query->num_rows())
		{
			return $query->row();
		}
		
		return FALSE;
	}
	
	function get_info_rule($where, $field = '')
	{
		$this->db->where($where);
		
	 	if ($field != '')
	 	{
	 		$this->db->select($field);
	 	}
	 	
		$query = $this->db->get($this->table);
		if ($query->num_rows())
		{
			return $query->row();
		}
		
		return FALSE;
	}
	
	function get_total($where = array())
	{
		if (count($where))
		{
			$this->db->where($where);
			$this->db->from($this->table);
			return $this->db->count_all_results();
		}
		
		return $this->db->count_all($this->table);
	}
	
	function get_list($input = array())
	{
        
		$this->get_list_set_input($input);
		
		$query = $this->db->get($this->table);
		
		$result =  $query->result();
	
        return $result;
	}
    
    function get_list_hash($input = array())
    {
        $result = $this->get_list($input);
        $hash = array();
        foreach($result as $id => $obj)
            $hash[$obj->{$this->primary_key}] = $obj;
            
        return $hash;
    }
	
	protected function get_list_set_input($input)
	{
		// Select
		$select = (isset($input['select'])) ? $input['select'] : '*';
		$this->db->select($select);
		
		// Where
		$where = (isset($input['where'])) ? $input['where'] : array();
		if (count($where))
		{
			$this->db->where($where);
		}
		
		// Order
		$order = (isset($input['order'])) ? $input['order'] : array();
		if (count($order))
		{
			$this->db->order_by($order[0], $order[1]);
		}
		else
		{
			$this->db->order_by($this->primary_key, 'desc');
		}
		
		// Limit
		$limit = (isset($input['limit'])) ? $input['limit'] : array();
		if (count($limit))
		{
			$this->db->limit($limit[1], $limit[0]);
		}
	}
}